package com.shanjupay.merchant.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shanjupay.common.exception.BusinessException;
import com.shanjupay.common.exception.CommonErrorCode;
import com.shanjupay.common.util.PhoneUtil;
import com.shanjupay.merchant.api.MerchantService;
import com.shanjupay.merchant.api.dto.MerchantDTO;
import com.shanjupay.merchant.convert.MerchantCovert;
import com.shanjupay.merchant.entity.Merchant;
import com.shanjupay.merchant.mapper.MerchantMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by Administrator.
 */
@org.apache.dubbo.config.annotation.Service
public class MerchantServiceImpl implements MerchantService {

    @Autowired
    MerchantMapper merchantMapper;

    @Override
    public MerchantDTO queryMerchantById(Long id) {
        Merchant merchant = merchantMapper.selectById(id);
        MerchantDTO merchantDTO = new MerchantDTO();
        //merchantDTO.setId(merchant.getId());
        //merchantDTO.setMerchantName(merchant.getMerchantName());
        //....
//        return merchantDTO;
        return null;
    }

    @Override
    @Transactional
    public MerchantDTO createMerchant(MerchantDTO merchantDTO) throws BusinessException {
        // 检验参数
        if (merchantDTO == null){
            throw new BusinessException(CommonErrorCode.E_100108);
        }
        // 手机号非空校验
        if (StringUtils.isBlank(merchantDTO.getMobile())){
            throw new BusinessException(CommonErrorCode.E_100112);
        }
        // 手机号的合法校验
        if (!PhoneUtil.isMatches(merchantDTO.getMobile())){
            throw new BusinessException(CommonErrorCode.E_100109);
        }
        // 密码非空校验
        if (StringUtils.isBlank(merchantDTO.getPassword())){
            throw new BusinessException(CommonErrorCode.E_100111);
        }
        // 校验手机号是否重复
        LambdaQueryWrapper<Merchant> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Merchant::getMobile,merchantDTO.getMobile());
        Integer count = merchantMapper.selectCount(wrapper);
        if (count>0){
            throw new BusinessException(CommonErrorCode.E_100113);
        }

        // 将dto转成entity
        Merchant merchant = MerchantCovert.INSTANCE.dto2entity(merchantDTO);
        // Merchant merchant = new Merchant();
        // 设置审核状态0‐未申请,1‐已申请待审核,2‐审核通过,3‐审核拒绝
        merchant.setAuditStatus("0");
        // 设置手机号
        // merchant.setMobile(merchantDTO.getMobile());
        // ...
        merchantMapper.insert(merchant);  // 保存商户

        // 将新增商户id返回,将entity转成dto
        // merchantDTO.setId(merchant.getId());
        return MerchantCovert.INSTANCE.entity2dto(merchant);
    }

    @Override
    public void applyMerchant(Long merchantId, MerchantDTO merchantDTO) throws BusinessException {
        // 校验
        if (merchantId==null || merchantDTO==null){
            throw new BusinessException(CommonErrorCode.E_110006);
        }
        // 根据id查询商户
        Merchant merchant = merchantMapper.selectById(merchantId);
        if (merchant==null){
            throw new BusinessException(CommonErrorCode.E_200002);
        }
        // 构造entity类,更新
        Merchant merchant_update = MerchantCovert.INSTANCE.dto2entity(merchantDTO);
        merchant_update.setId(merchant.getId());
        merchant_update.setTenantId(merchant.getTenantId());
        merchant_update.setMobile(merchant.getMobile());     // 因为资质申请手机号不能修改
        merchant_update.setTenantId(merchant.getTenantId());
        merchant_update.setAuditStatus("1");   // 已申请待审核
        merchantMapper.updateById(merchant_update);
    }
}
